<template>
  <div class="login-page">
    <cpNavBar rightText="注册"></cpNavBar>
    <div class="login-head">
      <h3>{{ isPass?'密码登录':'短信验证码登录' }}</h3>
      <a href="#" @click="isPass=!isPass">
        <span>{{ isPass?'短信验证码登录':'密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <van-form autocomplete="off" @submit="login">
      <van-field v-model="mobile" :rules="mobileRules" placeholder="请输入手机号" type="tel" />
      <van-field v-if="isPass" v-model="password" :rules="passwordRules" placeholder="请输入密码" type="password" />
      <van-field v-else v-model="code" :rules="codeRules" placeholder="请输入验证码" type="tel">
        <template #button><span @click="send">{{time>0?`${time}s后再发送`:'发送验证码'}}</span></template>
      </van-field>  
      <div class="cp-cell">
        <van-checkbox v-model="agreen"
          ><span>我已同意</span><a href="#">用户协议 </a><span>及</span
          ><a href="#">隐私条款</a></van-checkbox
        >
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">登录</van-button>
      </div>
      <div class="cp-cell">
        <a href="#">忘记密码?</a>
      </div>
    </van-form>
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/logo.svg" alt="" />
      </div>
    </div>
  </div>
</template>
<script setup>
import cpNavBar from "@/components/cpNavBar.vue";
import {mobileRules, passwordRules,codeRules} from '@/utils/rules.ts'
import { showToast,showSuccessToast } from "vant";
import {ref} from 'vue'
import {loginPasswordAPI,sendCodeAPI,loginCodeAPI} from '@/api/user'
import {useUserStore} from '@/stores'
import {useRouter,useRoute} from 'vue-router'
const mobile=ref('13230000001')
const password=ref()
const code=ref()
const agreen=ref()
const isPass=ref(true)
const store=useUserStore()
const router=useRouter()
const route=useRoute()
const login=async()=>{
    if(!agreen.value) return showToast('请勾选同意')

    const res=isPass.value
    ?await loginPasswordAPI(mobile.value,password.value)
    :await loginCodeAPI(mobile.value,code.value)
    console.log(res);
    store.setUser(res.data)
    showSuccessToast('登陆成功')
    router.push((route.query.returnUrl)||'/')
}
const time=ref(0)
const send=async()=>{
  if(time.value>0){
    return
  }
  const res=await sendCodeAPI(mobile.value,'login')
  console.log(res);
  showSuccessToast('验证码发送成功')
  time.value=60;
  let timeid;
  window.clearInterval(timeid)
  timeid=window.setInterval(()=>{
    time.value--;
    if(time.value<=0) window.clearInterval(timeid)
  },1000)
}
</script>
<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }

  &-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding: 30px, 30px, 50px;

    h3 {
      font-weight: normal;
      font-size: 24px;
    }

    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 50px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 50px;
        height: 50px;
        padding: 5px;
      }
    }
  }
}

.van-form {
  padding: 40px 14px;

  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 18px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
}
</style>
